#include <cassert>
#include <fstream>
#include "Features.h"
#ifdef Q_WS_MAEMO_5
 #include "FeaturesMaemo.h"
#endif
#include <opencv/highgui.h>
#include <exception>
#include <QApplication>

void saveRocFile ( const char *       filename,
                   vector<float> &    thresholdList,
                   vector<ROCPoint> & results );
void saveAUCFile ( const char *       filename,
                   vector<float> &    thresholdList,
                   vector<ROCPoint> & results );

void convertToFloatImage ( IplImage * byteImage, IplImage * floatImage )
{
  cvConvertScale ( byteImage, floatImage, 1 / 255., 0 );
}

class FeatureError : public std::exception
{
std::string message;
public:
FeatureError ( const std::string & msg, const std::string & file )
{
  std::stringstream ss;

  ss << msg << " " << file;
  message = msg.c_str ();
}
~FeatureError () throw ( )
{
}
};

IplImage * loadByteImage ( const char * name )
{
  IplImage * image = cvLoadImage ( name );

  if ( !image )
  {
    return 0;
  }
  cvCvtColor ( image, image, CV_BGR2RGB );
  return image;
}

IplImage * loadFloatImage ( const char * name )
{
  IplImage * image = cvLoadImage ( name );

  if ( !image )
  {
    return 0;
  }
  cvCvtColor ( image, image, CV_BGR2RGB );
  IplImage * floatingImage = cvCreateImage ( cvGetSize ( image ),
                                             IPL_DEPTH_32F, 3 );
  cvConvertScale ( image, floatingImage, 1 / 255., 0 );
  cvReleaseImage ( &image );
  return floatingImage;
}

int mainPanorama ( int argc, char * * argv )
{
  if ( argc < 5 || argc > 7 )
  {
    printf (
      "usage: %s panorama imageFile1 imageFile2 outputImageFile [featureType] [matchType]\n",
      argv[0] );
    return -1;
  }
  IplImage * img1 = loadFloatImage ( argv[2] );
  if ( !img1 )
  {
    return -1;
  }
  IplImage * img2 = loadFloatImage ( argv[3] );
  if ( !img2 )
  {
    return -1;
  }
  int featureType = 0;
  int matchType = 0;
  if ( argc > 5 )
  {
    featureType = atoi ( argv[5] );
  }
  if ( !featureType )
  {
    featureType = 3;
  }
  if ( argc > 6 )
  {
    matchType = atoi ( argv[6] );
  }
  if ( !matchType )
  {
    matchType = 2;
  }
  IplImage * output = constructPanorama ( img1, img2, featureType, matchType );
  cvReleaseImage ( &img1 );
  cvReleaseImage ( &img2 );
  cvConvertScale ( output, output, 255, 0 );
  cvCvtColor ( output, output, CV_RGB2BGR );
  cvSaveImage ( argv[4], output );
  cvReleaseImage ( &output );
  return 0;
}

void printusage(char * * argv)
{
   printf ( "usage:\n" );
   printf ( "\t%s\n", argv[0] );
   printf ( "\t%s panorama imageFile1 imageFile2 outputImageFile\n",
            argv[0] );
}

int main ( int argc, char * * argv )
{
  if ( argc > 1 )
  {
    if ( strcmp ( argv[1], "panorama" ) == 0 )
    {
      mainPanorama ( argc, argv );
    }
    else
    {
      printusage(argv);
      return -1;
    }
  }
  else
  {
#ifdef Q_WS_MAEMO_5
    QApplication app ( argc, argv );
    // Use the GUI.
    QWidget * ui = new FeaturesMaemo;
    app.setActiveWindow ( ui );
    ui->show ();
    return app.exec ();
#else
    // no gui for desktop
    printusage(argv);
    return -1;
#endif
  }
}
